//
//  TypeViewController.m
//  TourPlay
//
//  Created by dllo on 15/11/16.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "TypeViewController.h"
#import "TypeView.h"
#import "TypeViewCollectionCell.h"
#import "ActivitieDetailsViewController.h"

@interface TypeViewController () <UICollectionViewDataSource, UICollectionViewDelegate, TypeDelegate, TypeViewCollectionCellDelegate>

@property (nonatomic, retain) MBProgressHUD *hub; // 加载转圈
@property (nonatomic, retain) NSMutableArray *titleArr; // 标题数组

@property (nonatomic, retain) TypeView *typeView; // 标题
@property (nonatomic, retain) UICollectionView *myCollectionV; // 左右滑
@property (nonatomic, retain) TypeViewCollectionCell *collectionCell;

@property (nonatomic, assign) BOOL btnSelected;

@property (nonatomic, retain) UIView *navigationHiddenView;

@end

@implementation TypeViewController

- (void)dealloc
{
    [_hub release];
    [_titleArr release];
    [_typeView release];
    [_myCollectionV release];
    [_navigationHiddenView release];
    [super dealloc];
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self navigationViewWithIsHidden:NO];
//    [self.myCollectionV reloadData];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];
    self.titleArr = [NSMutableArray array];

    self.navigationHiddenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    self.navigationHiddenView.backgroundColor = [UIColor redColor];
    
     self.navigationItem.leftBarButtonItem = nil;
    
//    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置每个item(cell)的大小
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64);
    // 设置最小行间距
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);

    // 滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.myCollectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64) collectionViewLayout:flowLayout];
    self.myCollectionV.delegate = self;
    self.myCollectionV.dataSource = self;
    // 分页滚动
    self.myCollectionV.pagingEnabled = YES;
    // 隐藏滑动条
    self.myCollectionV.showsHorizontalScrollIndicator = NO;
    
    self.myCollectionV.backgroundColor = [UIColor clearColor];
    [self.myCollectionV registerClass:[TypeViewCollectionCell class] forCellWithReuseIdentifier:@"CollectionCell"];
    [self.view addSubview:self.myCollectionV];
    [_myCollectionV release];
    [flowLayout release];
    
    
    
    UIView *buttonBack = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 35, 0, 35, 44)];
    buttonBack.backgroundColor = [UIColor orangeColor];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:buttonBack.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(4, 4)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = buttonBack.bounds;
    maskLayer.path = maskPath.CGPath;
    buttonBack.layer.mask = maskLayer;
    [maskLayer release];
    [self.view addSubview:buttonBack];
    [buttonBack release];
    
    [self addColorChangedBlock:^{
        
        buttonBack.nightBackgroundColor = [ColorSingleton shareSingleton].viewBackgroundColor;
        buttonBack.normalBackgroundColor = [ColorSingleton shareSingleton].viewBackgroundColor;
    }];
    
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];
//    button.frame = CGRectMake(SCREEN_WIDTH - 30, 10, 22, 22);
    button.frame = buttonBack.frame;
    button.backgroundColor = [UIColor clearColor];
    [button setImage:[UIImage imageNamed:@"iconfont-shupai.png"] forState:UIControlStateNormal];
//    [button setImage:[UIImage imageNamed:@"iconfont-shupai.png"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"iconfont-hengpai.png"] forState:UIControlStateSelected];
//    [button setImage:[UIImage imageNamed:@"iconfont-hengpai.png"] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button.layer.cornerRadius = 5.f;
    [self.view addSubview:button];
    
    button.selected = YES;
    self.btnSelected = YES;
    [self upDate];
    
    
    // 接收城市改变通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(homeRefurbish:) name:@"homeRefurbish" object:nil];
}
#pragma mark - 城市改变通知
- (void)homeRefurbish:(NSNotification *)sender
{
//    [self.myCollectionV reloadData];
}
- (void)upDate
{
    // 让菊花旋转起来
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"努力加载中...";
    [self getUpdateWithURL];
}
#pragma mark - 切换视图
- (void)buttonAction:(UIButton *)sender
{
    sender.selected = !sender.selected;
    self.btnSelected = sender.selected;

    [self.collectionCell hiddenMicroCellWithIsHidden:self.btnSelected];
}

- (TypeView *)typeView
{
    if (nil == _typeView) {
        self.typeView = [[TypeView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        self.typeView.delegate = self;
    }
    return _typeView;
}

- (void)changeCollectionViewWithNum:(NSInteger)num
{
//    NSLog(@"---- %ld", num);
    [self.myCollectionV setContentOffset:CGPointMake(num * SCREEN_WIDTH, 0) animated:NO];
}
#pragma mark - Collection
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.titleArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"CollectionCell";
    self.collectionCell = [collectionView dequeueReusableCellWithReuseIdentifier:cellStr forIndexPath:indexPath];
    self.collectionCell.delegate = self;
    [self.collectionCell hiddenMicroCellWithIsHidden:self.btnSelected];
    TypeTitleModel *titleMod = [self.titleArr objectAtIndex:indexPath.row];
    self.collectionCell.titleMod = titleMod;

    
    self.collectionCell.backgroundColor = [UIColor clearColor];
    return _collectionCell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
     NSInteger num = self.myCollectionV.contentOffset.x / SCREEN_WIDTH;
    
    
    [self.typeView changemyScrollView:num btnTag:1000 + num];
    [self.typeView changeButtonColorWithTag:1000 + num];

}
#pragma mark - 跳转到活动页面
- (void)goToActivityWithId:(NSString *)activityId activityTitle:(NSString *)title
{
    ActivitieDetailsViewController *activitieVC = [[ActivitieDetailsViewController alloc] init];
    activitieVC.activityId = activityId;
    activitieVC.activityTitle = title;
    // 隐藏
    activitieVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:activitieVC animated:YES];
    [activitieVC release];
}
#pragma mark 隐藏navigation
- (void)navigationViewWithIsHidden:(BOOL)isHidden
{
    self.typeView.hidden = isHidden;
}
#pragma mark - 获取标题数据
- (void)getUpdateWithURL
{
    NSString *url = [NSString stringWithFormat:@"http://wanapi.damai.cn/filterparamnew.json?source=%d&version=%d", URL_SOURCE, URL_VERSION];

    [AppTools getAFNet:url block:^(id result) {
        [self.typeView.titleArr removeAllObjects];
        
        NSMutableDictionary *bigDic = result;
        NSMutableArray *categoryArr = [[bigDic objectForKey:@"data"] objectForKey:@"CategoryDoc"];
        for (NSMutableDictionary *dic in categoryArr) {
            TypeTitleModel *titleMod = [TypeTitleModel modelWithDictionary:dic];
            [self.titleArr addObject:titleMod];
        }
        
        [self.navigationController.navigationBar addSubview:self.typeView];
        [self.navigationController.navigationBar addSubview:self.navigationHiddenView];
        self.navigationHiddenView.hidden = YES;
        [_navigationHiddenView release];
        
        self.typeView.titleArr = self.titleArr;
        [_typeView release];

        
        [self.myCollectionV reloadData];
        
        // 让菊花停止旋转
        [self.hub hide:YES];
    } errorBlock:^(NSError *error) {
        NSLog(@"网络连接错误");
        [self NetworkError:^{
        }];
        
        //结束下拉上拉刷新
        [self.myCollectionV.header endRefreshing];
        [self.myCollectionV.footer endRefreshing];
        [self.hub hide:YES];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
